<?php

/**
 * Joomla! 1.5 component retweet
 * @version 0.1
 * @author Chris Borkowski
 * @package com_retweet
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 **/

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');
/**
 * HTML View class for the retweet component
 *
 * @static
 * @package Retweet
 */
class RetweetViewItem extends JView {
	function display($tpl = null) {
		global $mainframe;
		if ($this -> getLayout() == 'form') {
			$this -> _displayForm($tpl);
			return;
		}
		//get the item
		$item = &$this -> get('data');
		parent::display($tpl);
	}

	function _displayForm($tpl) {
		global $mainframe, $option;
		$db = &JFactory::getDBO();
		$uri = &JFactory::getURI();
		$user = &JFactory::getUser();
		$model = &$this -> getModel('item');
		$lists = array();
		RetweetHelper::checkCategories();
		//get the item
		$item = &$this -> get('data');
		$isNew = ($item -> id < 1);
		// fail if checked out not by 'me'
		if ($model -> isCheckedOut($user -> get('id'))) {
			$msg = JText::sprintf('DESCBEINGEDITTED', JText::_('The list'), $item -> title);
			$mainframe -> redirect('index.php?option=' . $option, $msg);
		}
		// Edit or Create?
		if (!$isNew) {
			$model -> checkout($user -> get('id'));
		} else {
			// initialise new record
			$item -> published = 1;
			$item -> order = 0;
			$item -> catid = JRequest::getVar('catid', 0, 'post', 'int');
		}
		// build the html select list for ordering
		$query = 'SELECT ordering AS value, title AS text' . ' FROM #__retweet' . ' WHERE catid = ' . (int)$item -> catid . ' ORDER BY ordering';
		$lists['ordering'] = JHTML::_('list.specificordering', $item, $item -> id, $query, 1);
		$lists['catid'] = JHTML::_('list.category', 'catid', $option, intval($item -> catid));
		$lists['published'] = JHTML::_('select.booleanlist', 'published', 'class="inputbox"', $item -> published);
		//build the html radio buttons for all the custom bolean fields
		$lists['use_new_body'] = JHTML::_('select.booleanlist', 'use_new_body', 'class="inputbox"', $item -> use_new_body);
		$lists['use_profile_override'] = JHTML::_('select.booleanlist', 'use_profile_override', 'class="inputbox"', $item -> use_profile_override);
		$lists['link_to_profile'] = JHTML::_('select.booleanlist', 'link_to_profile', 'class="inputbox"', $item -> link_to_profile);
		$lists['override_source'] = JHTML::_('select.booleanlist', 'override_source', 'class="inputbox"', $item -> override_source);
		$lists['retweet'] = JHTML::_('select.booleanlist', 'retweet', 'class="inputbox"', $item -> retweet);
		$lists['local_publish'] = JHTML::_('select.booleanlist', 'local_publish', 'class="inputbox"', $item -> local_publish);
		$lists['publish_to_facebok'] = JHTML::_('select.booleanlist', 'publish_to_facebok', 'class="inputbox"', $item -> publish_to_facebok);
		$lists['embed_code'] = JHTML::_('select.booleanlist', 'embed_code', 'class="inputbox"', $item -> embed_code);
		$lists['include_media'] = JHTML::_('select.booleanlist', 'include_media', 'class="inputbox"', $item -> include_media);
		$lists['mail_sent'] = JHTML::_('select.booleanlist', 'mail_sent', 'class="inputbox"', $item -> mail_sent);
		$lists['send_again'] = JHTML::_('select.booleanlist', 'send_agian', 'class="inputbox"', $item -> send_again);
		
		
		//clean item data
		jimport('joomla.filter.output');
		JFilterOutput::objectHTMLSafe($item, ENT_QUOTES, 'text');
		$file = JPATH_COMPONENT . DS . 'models' . DS . 'item.xml';
		$params = new JParameter($item -> params, $file);
		
		//load the editor
		$editor =& JFactory::getEditor();
		
		//push to form
		$this->assignRef('editor', $editor);
		$this -> assignRef('lists', $lists);
		$this -> assignRef('item', $item);
		$this -> assignRef('params', $params);
		parent::display($tpl);
	}

}
